package com.fhx.core.utils

import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.os.LocaleList
import android.text.TextUtils
import java.util.*

object AppLanguageUtils {
    const val LANGUAGE = "language"
    const val CHINESE = "cn"
    const val ENGLISH = "en"
    private val mAllLanguages = mapOf<String, Locale>(
        Pair(ENGLISH, Locale.ENGLISH),
        Pair(CHINESE, Locale.SIMPLIFIED_CHINESE)
    )
//        object : HashMap<String?, Locale?>(8) {
//            init {
//                put(ENGLISH, Locale.ENGLISH)
//                put(CHINESE, Locale.SIMPLIFIED_CHINESE)
    //        put(Constants.SIMPLIFIED_CHINESE, Locale.SIMPLIFIED_CHINESE);
//        put(Constants.TRADITIONAL_CHINESE, Locale.TRADITIONAL_CHINESE);
//        put(Constants.FRANCE, Locale.FRANCE);
//        put(Constants.GERMAN, Locale.GERMANY);
//        put(Constants.HINDI, new Locale(Constants.HINDI, "IN"));
//        put(Constants.ITALIAN, Locale.ITALY);
//            }
//        }

    fun changeAppLanguage(
        context: Context,
        newLanguage: String?
    ) {
        val resources = context.resources
        val configuration = resources.configuration

        // app locale
        val locale = getLocaleByLanguage(newLanguage)
        configuration.setLocale(locale)

        // updateConfiguration
        val dm = resources.displayMetrics

        resources.updateConfiguration(configuration, dm)
    }

    private fun isSupportLanguage(language: String?): Boolean {
        return mAllLanguages.containsKey(language)
    }

    fun getSupportLanguage(language: String?): String? {
        if (isSupportLanguage(language)) {
            return language
        }
        if (null == language) { //为空则表示首次安装或未选择过语言，获取系统默认语言
            val locale = Locale.getDefault()
            for (key in mAllLanguages.keys) {
                if (TextUtils.equals(mAllLanguages[key]!!.language, locale.language)) {
                    return locale.language
                }
            }
        }
        return ENGLISH
    }

    /**
     * 获取指定语言的locale信息，如果指定语言不存在[.mAllLanguages]，返回本机语言，如果本机语言不是语言集合中的一种[.mAllLanguages]，返回英语
     *
     * @param language language
     * @return
     */
    private fun getLocaleByLanguage(language: String?): Locale? {
        if (isSupportLanguage(language)) {
            return mAllLanguages[language]
        } else {
            val locale = Locale.getDefault()
            for (key in mAllLanguages.keys) {
                if (TextUtils.equals(
                        mAllLanguages[key]!!.language, locale.language
                    )
                ) {
                    return locale
                }
            }
        }
        return Locale.ENGLISH
    }

    fun attachBaseContext(
        context: Context,
        language: String
    ): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else {
            context
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResources(
        context: Context,
        language: String
    ): Context {
        val resources = context.resources
        val locale = getLocaleByLanguage(language)
        val configuration = resources.configuration
        configuration.setLocale(locale)
        configuration.setLocales(LocaleList(locale))
        return context.createConfigurationContext(configuration)
    }
}